   package com.javalong.affairs.module.notifything.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.javalong.affairs.R;
import com.javalong.affairs.module.notifything.adapter.TreeViewAdapter;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.DataType;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.fragment.BaseFragment;
import com.javalong.affairs.module.schedule.fragment.ScheduleListFragment;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.model.TreeElement;
import com.javalong.affairs.base.model.TreeNode;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.UrlUtils;

public class DeptFragment extends BaseFragment {
	private ListView lv_tree;
	private TreeViewAdapter treeViewAdapter;
	private ArrayList<TreeElement> mRootList;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_deptlist, null);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		lv_tree = (ListView) view.findViewById(R.id.lv_tree);
		mRootList = new ArrayList<TreeElement>();
		treeViewAdapter = new TreeViewAdapter(getActivity(),
				R.layout.atom_tree, mRootList);
		lv_tree.setAdapter(treeViewAdapter);
		lv_tree.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				TreeElement elem = mRootList.get(position);
				if (elem.getType() == 1) {
					Bundle bundle = new Bundle();
					bundle.putInt(BundleKey.USERID,
							Integer.parseInt(elem.getValue()));
					bundle.putBoolean(BundleKey.NODELETE,true);
					FragmentTransaction tranction = getFragmentManager()
							.beginTransaction();
					Fragment fragment = new ScheduleListFragment();
					fragment.setArguments(bundle);
					tranction.replace(R.id.fl_notify, fragment);
					tranction.addToBackStack(null);
					tranction.commit();
					return;
				}

				if (!mRootList.get(position).isHasChild()) {
					Toast.makeText(getActivity(),
							mRootList.get(position).getCaption(),
							Toast.LENGTH_SHORT).show();
					return;
				}

				if (mRootList.get(position).isExpanded()) {
					mRootList.get(position).setExpanded(false);
					TreeElement element = mRootList.get(position);
					ArrayList<TreeElement> temp = new ArrayList<TreeElement>();

					for (int i = position + 1; i < mRootList.size(); i++) {
						if (element.getLevel() >= mRootList.get(i).getLevel()) {
							break;
						}
						temp.add(mRootList.get(i));
					}
					mRootList.removeAll(temp);
					treeViewAdapter.notifyDataSetChanged();
				} else {
					// 展开
					TreeElement obj = mRootList.get(position);
					obj.setExpanded(true);
					int level = obj.getLevel();
					int nextLevel = level + 1;
					expandTreeByDeptId(obj, obj.getValue(), nextLevel, position);
				}
			}
		});
		User user = (User) AllData.GetInstance().get(
				R.string.data_user_current_user);
		expandTreeByDeptId(mRootList, user.section, 0, 0);
	}

	private void expandTreeByDeptId(final Object element, String deptId,
			final int nextLevel, final int position) {
		String url = UrlUtils.getUrl("Journal", "outList_pointList_section");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(RequestKey.ID, deptId);
		HttpUtils.post(url, params, new BaseResponseListener(
				DataType.TREENODELIST) {
			@Override
			public void onSuccess(Object obj) {
				super.onSuccess(obj);
				List<TreeNode> nodes = (List<TreeNode>) obj;
				if (nodes == null) {
					return;
				}
				for (TreeNode node : nodes) {
					TreeElement elem = null;
					switch (node.type) {
					case 0:// 部门
						elem = new TreeElement("" + node.id, node.name, ""
								+ node.id, true, false, node.type);
						break;
					case 1:// 用户
						elem = new TreeElement("" + node.id, node.name + "("
								+ node.level + ")", "" + node.id, false, false,
								node.type);
						break;
					}
					if (element instanceof TreeElement) {
						TreeElement e = (TreeElement) element;
						e.addChild(elem);
						elem.setLevel(nextLevel);
						elem.setExpanded(false);
						mRootList.add(position + 1, elem);
					} else if (element instanceof List) {
						ArrayList<TreeElement> list = (ArrayList<TreeElement>) element;
						list.add(elem);
					} else {
						return;
					}

				}
				treeViewAdapter.notifyDataSetChanged();
			}
		});
	}
}